package nl.boektrust.btsoep.client;

import java.util.HashSet;

import nl.boektrust.btsoep.shared.FieldVerifier;
import nl.boektrust.btsoep.shared.model.Person;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Btsoep implements EntryPoint, ValueChangeHandler<String> {
	final Label lbl = new Label("label");
	final DockLayoutPanel p = new DockLayoutPanel(Unit.EM);
	
	final SimplePanel btAboutPanel = new SimplePanel();
	final BoekenPanel btBoekenPanel = new BoekenPanel(this);
	final MensenPanel btMensenPanel = new MensenPanel(this);
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
    private final PersistentServiceAsync persistentService = GWT.create(PersistentService.class);

    private HashSet<Person> storedPersonsList = new HashSet<Person>();
    
    private StoredPersonsPanel storedPersonsPanel;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
	    // Attach five widgets to a DockLayoutPanel, one in each direction. Lay
	    // them out in 'em' units.
		storedPersonsPanel = new StoredPersonsPanel(this);
	    
	    p.addNorth(new HTML("Boektrust SOEP"), 10);
	    p.addSouth(new HTML(""), 10);
	    p.addEast(storedPersonsPanel, 10);
	    VerticalPanel westPanel = new VerticalPanel();
	    westPanel.add(new Hyperlink("BoekTrust", "BoekTrust"));
	    westPanel.add(new Hyperlink("Boeken", "Boeken"));
	    westPanel.add(new Hyperlink("Mensen", "Mensen"));
	    p.addWest(westPanel, 10);
	    btMensenPanel.persistentService = persistentService;
	    btBoekenPanel.persistentService = persistentService;
	    

	    //p.add(lbl);
	    btAboutPanel.add(new AboutPanel());

	    // Attach the LayoutPanel to the RootLayoutPanel. The latter will listen for
	    // resize events on the window to ensure that its children are informed of
	    // possible size changes.
	    RootLayoutPanel rp = RootLayoutPanel.get();
	    rp.add(p);
	    
	    // If the application starts with no history token, redirect to a new
	    // 'baz' state.
	    String initToken = History.getToken();
	    if (initToken.length() == 0) {
	      History.newItem("BoekTrust");
	    }
	    
	 // Add history listener
	    History.addValueChangeHandler(this);

	    // Now that we've setup our listener, fire the initial history state.
	    History.fireCurrentHistoryState();
	}
	
	public void onValueChange(ValueChangeEvent<String> event) {
	    // This method is called whenever the application's history changes. Set
	    // the label to reflect the current history token.
	    lbl.setText("The current history token is: " + event.getValue());
    	btAboutPanel.removeFromParent();
    	btBoekenPanel.removeFromParent();
    	btMensenPanel.removeFromParent();
	    if (event.getValue().equals("Boeken")) {
	    	p.add(btBoekenPanel);
	    	storedPersonsPanel.setVisible(false);
	    } else if (event.getValue().equals("BoekTrust")) {
	    	p.add(btAboutPanel);
	    	storedPersonsPanel.setVisible(false);
	    } else if (event.getValue().equals("Mensen")) {
	    	p.add(btMensenPanel);
	    	storedPersonsPanel.setVisible(true);
	    }
	  }

	/**
	 * @return the storedPersonsList
	 */
	public HashSet<Person> getStoredPersonsList() {
		return storedPersonsList;
	}

	public void addPersonToStoredPersonsList(Person p) {
		System.out.println("btsoep: add person to stored personslist called in btsoep.");
		this.storedPersonsList.add(p);
		storedPersonsPanel.refresh();
		
	}
	/**
	 * @param storedPersonsList the storedPersonsList to set
	 */
	public void setStoredPersonsList(HashSet<Person> storedPersonsList) {
		this.storedPersonsList = storedPersonsList;
	}
}
